﻿//using NP.Utilities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace NP.WebAPI.Models.Response
{
    /// <summary>
    /// 服务器响应基类信息
    /// </summary>
    public class BaseResponse<T>
    {
        /// <summary>
        /// 请求操作结果(1-成功，负数-失败)
        /// </summary>
        private int _result = 1;
        /// <summary>
        /// 请求错误编码,当_result=1时，错误编码无效
        /// </summary>
        private int _errorCode = -1;
        /// <summary>
        /// 返回信息
        /// </summary>
        private string _msg = string.Empty;
        /// <summary>
        /// 服务器返回请求数据信息集合
        /// </summary>
        private T _data = default(T);
        /// <summary>
        /// 服务器当前时间
        /// </summary>
        private string _time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
        /// <summary>
        /// Data数据总量
        /// </summary>
        private int _count = 1;

        /// <summary>
        /// 请求操作结果(1-成功，负数-失败)
        /// </summary>
        public int Result
        {
            get { return _result; }
            set { _result = value; }
        }

        /// <summary>
        /// 请求错误编码,当_result=1时，错误编码无效
        /// </summary>
        public int ErrorCode
        {
            get { return _errorCode; }
            set { _errorCode = value; }
        }
        /// <summary>
        /// 返回信息
        /// </summary>
        public string Msg
        {
            get { return _msg; }
            set { _msg = value; }
        }

        /// <summary>
        /// 服务器返回请求数据信息集合
        /// </summary>
        public T Data
        {
            get { return _data; }
            set { _data = value; }
        }
        /// <summary>
        /// 服务器当前时间
        /// </summary>
        public string ServerTime
        {
            //get { return Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); }
            get { return _time; }
            set { _time = value; }
        }
        /// <summary>
        /// Data数据总量（分页查询时，返回指定查询条件下能返回的最大数量）
        /// </summary>
        public int TotalCount
        {
            get { return _count; }
            set { _count = value; }
        }

        /// <summary>
        /// 返回结果异常处理
        /// </summary>
        /// <param name="exception">异常参数</param>
        public void ExceptionHandle(Exception exception)
        {
            this.Msg = exception.ToString();
            this.ErrorCode = 1;
            this.Result = -1;
            this.TotalCount = 0;

            //LogHelper.Error( WriteLogType.LogError, exception.ToString());
        }

        /// <summary>
        /// 操作成功
        /// </summary>
        /// <param name="data">获取数据</param>
        /// <param name="TotalCount">数据总量</param>
        /// <param name="message">返回信息</param>
        public void SuccessHandle(T data,int TotalCount,string message)
        {
            this.Msg = message;
            this.ErrorCode = -1;
            this.Result = 1;
            this.TotalCount = TotalCount;
            this.Data = data;
        }
    }
}